#ifndef _CRANE_JPUC_COMMON_H_
#define _CRANE_JPUC_COMMON_H_

#ifdef __cplusplus
extern "C" {
#endif

#include "lv_drv_conf.h"

#if USE_CRANE_JPU
#include "stdint.h"

#define IRQ_MMP_CAM_JPEG (63)

#define MJPEG_HW_IRQ_STATUS_CODEC_DONE    (0x1 << 0)
#define MJPEG_HW_IRQ_STATUS_DECODE_ERROR  (0x1 << 1)
#define MJPEG_HW_IRQ_STATUS_BBC_INTERRUPT (0x1 << 2)
#define MJPEG_HW_IRQ_STATUS_PARTIAL_OVERFLOW (0x1 << 3)
#define MJPEG_HW_IRQ_STATUS_PARTIAL_BUF0  (0x1 << 4)
#define MJPEG_HW_IRQ_STATUS_PARTIAL_BUF1  (0x1 << 5)
#define MJPEG_HW_IRQ_STATUS_PARTIAL_BUF2  (0x1 << 6)
#define MJPEG_HW_IRQ_STATUS_PARTIAL_BUF3  (0x1 << 7)
#define MJPEG_HW_IRQ_STATUS_CODEC_STOP    (0x1 << 8)
#define MJPEG_HW_IRQ_STATUS_CONFIG_DONE   (0x1 << 9)
#define MJPEG_HW_IRQ_STATUS_CODEC_START   (0x1 << 10)

//===================================================================================
// NIEUPORT REGISTERS
//===================================================================================
// MBC
#define MJPEG_PIC_START_REG   0x0000
#define MJPEG_PIC_STATUS_REG  0x0004
#define MJPEG_PIC_ERRMB_REG   0x0008
#define MJPEG_PIC_SETMB_REG   0x000C

#define MJPEG_PIC_CTRL_REG   0x0010
#define MJPEG_PIC_SIZE_REG   0x0014
#define MJPEG_MCU_INFO_REG   0x0018
#define MJPEG_ROT_INFO_REG   0x001C

#define MJPEG_SCL_INFO_REG   0x0020
#define MJPEG_IF_INFO_REG   0x0024
#define MJPEG_CLP_INFO_REG   0x0028
#define MJPEG_OP_INFO_REG   0x002C

#define MJPEG_DPB_CONFIG_REG  0x0030
#define MJPEG_DPB_BASE00_REG  0x0034
#define MJPEG_DPB_BASE01_REG  0x0038
#define MJPEG_DPB_BASE02_REG  0x003C

#define MJPEG_DPB_BASE10_REG  0x0040
#define MJPEG_DPB_BASE11_REG  0x0044
#define MJPEG_DPB_BASE12_REG  0x0048
#define MJPEG_DPB_BASE20_REG  0x004C

#define MJPEG_DPB_BASE21_REG  0x0050
#define MJPEG_DPB_BASE22_REG  0x0054
#define MJPEG_DPB_BASE30_REG  0x0058
#define MJPEG_DPB_BASE31_REG  0x005C

#define MJPEG_DPB_BASE32_REG  0x0060
#define MJPEG_DPB_YSTRIDE_REG  0x0064
#define MJPEG_DPB_CSTRIDE_REG  0x0068
#define MJPEG_WRESP_CHECK_REG  0x006C

#define MJPEG_CLP_BASE_REG          0x0070
#define MJPEG_CLP_SIZE_REG          0x0074

#define MJPEG_HUFF_CTRL_REG   0x0080
#define MJPEG_HUFF_ADDR_REG   0x0084
#define MJPEG_HUFF_DATA_REG   0x0088

#define MJPEG_QMAT_CTRL_REG   0x0090
#define MJPEG_QMAT_ADDR_REG   0x0094
#define MJPEG_QMAT_DATA_REG   0x0098

#define MJPEG_COEF_CTRL_REG   0x00A0
#define MJPEG_COEF_ADDR_REG   0x00A4
#define MJPEG_COEF_DATA_REG   0x00A8

#define MJPEG_RST_INTVAL_REG  0x00B0
#define MJPEG_RST_INDEX_REG   0x00B4
#define MJPEG_RST_COUNT_REG   0x00B8

#define MJPEG_INTR_MASK_REG   0x00C0
#define MJPEG_CYCLE_INFO_REG  0x00C8

#define MJPEG_DPCM_DIFF_Y_REG  0x00F0
#define MJPEG_DPCM_DIFF_CB_REG  0x00F4
#define MJPEG_DPCM_DIFF_CR_REG  0x00F8

// GBU
#define MJPEG_GBU_CTRL_REG   0x0100
#define MJPEG_GBU_PBIT_BUSY_REG  0x0104

#define MJPEG_GBU_BT_PTR_REG  0x0110
#define MJPEG_GBU_WD_PTR_REG  0x0114
#define MJPEG_GBU_TT_CNT_REG  0x0118

#define MJPEG_GBU_PBIT_08_REG  0x0120
#define MJPEG_GBU_PBIT_16_REG  0x0124
#define MJPEG_GBU_PBIT_24_REG  0x0128
#define MJPEG_GBU_PBIT_32_REG  0x012C

#define MJPEG_GBU_BBSR_REG   0x0140
#define MJPEG_GBU_BBER_REG   0x0144
#define MJPEG_GBU_BBIR_REG   0x0148
#define MJPEG_GBU_BBHR_REG   0x014C

#define MJPEG_GBU_BCNT_REG   0x0158

#define MJPEG_GBU_FF_RPTR_REG  0x0160
#define MJPEG_GBU_FF_WPTR_REG  0x0164

// BBC
#define MJPEG_BBC_END_ADDR_REG  0x0208
#define MJPEG_BBC_WR_PTR_REG  0x020C
#define MJPEG_BBC_RD_PTR_REG  0x0210

#define MJPEG_BBC_EXT_ADDR_REG  0x0214
#define MJPEG_BBC_INT_ADDR_REG  0x0218
#define MJPEG_BBC_DATA_CNT_REG  0x021C
#define MJPEG_BBC_COMMAND_REG  0x0220
#define MJPEG_BBC_BUSY_REG   0x0224

#define MJPEG_BBC_CTRL_REG   0x0228
#define MJPEG_BBC_CUR_POS_REG  0x022C

#define MJPEG_BBC_BAS_ADDR_REG  0x0230
#define MJPEG_BBC_STRM_CTRL_REG  0x0234

#define MJPEG_BBC_FLUSH_CMD_REG  0x0238

// shadow
#define SD_MJPEG_BBC_END_ADDR_REG   0x0300
#define SD_MJPEG_BBC_WR_PTR_REG     0x0304
#define SD_MJPEG_BBC_RD_PTR_REG     0x0308
#define SD_MJPEG_BBC_EXT_ADDR_REG   0x030C
#define SD_MJPEG_BBC_INT_ADDR_REG   0x0310
#define SD_MJPEG_BBC_DATA_CNT_REG   0x0314
#define SD_MJPEG_BBC_CUR_POS_REG    0x0318
#define SD_MJPEG_BBC_BAS_ADDR_REG   0x031c

#define SD_MJPEG_GBU_BPTR_REG       0x0320
#define SD_MJPEG_GBU_WPTR_REG       0x0324
#define SD_MJPEG_GBU_BBSR_REG       0x0328
#define SD_MJPEG_GBU_BBER_REG       0x032c
#define SD_MJPEG_GBU_BBIR_REG       0x0330
#define SD_MJPEG_GBU_BBHR_REG       0x0334

#define SD_MJPEG_GBU_BT_PTR_REG    0x0320
#define SD_MJPEG_GBU_WD_PTR_REG    0x0324

#define SD_MJPEG_GBU_BBSR_REG    0x0328
#define SD_MJPEG_GBU_BBER_REG    0x032c
#define SD_MJPEG_GBU_BBIR_REG    0x0330
#define SD_MJPEG_GBU_BBHR_REG    0x0334

#define SD_MJPEG_PIC_SIZE_REG    0x0340
#define SD_MJPEG_PIC_CON_ENC_REG    0x0344
#define SD_MJPEG_SHWREG_RDY         0x0348

#define LAST_MJPEG_BBC_WR_PTR_REG     0x0350
#define LAST_MJPEG_BBC_RD_PTR_REG     0x0354

#define JPU_TIMEOUT         200

#ifndef MAX_FLAG_NUM
#define MAX_FLAG_NUM 64
#endif

enum jpu_state {
    JPU_STATE_BOOT,
    JPU_STATE_IDLE,
    JPU_STATE_ENC_ACTIVE,
    JPU_STATE_DEC_ACTIVE,
    JPU_STATE_OFF,
};

struct jpu_device {
    enum jpu_state state;
    uint32_t irq;
    uint32_t jpu_open_cnt;
};
extern struct jpu_device jpu_dev;

extern uint8_t jpeg_encode_flag;
extern uint8_t jpeg_decode_flag;
extern uint16_t jpeg_decode_reg;

void media_clk_jpu_on(void);
void media_clk_jpu_off(void);
void jpu_clk_reset(void);
void jpu_irq_enable(void);
void jpu_irq_disable(void);
int jpu_common_init(void);

#endif /* USE_CRANE_JPU */

#ifdef __cplusplus
} /* extern "C" */
#endif

#endif

